package cn.cloudreal.cbms.resourceState.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.resmonitor.dao.ResMonitorDao;
import cn.cloudreal.cbms.resourceState.dao.ResStateDao;
import cn.cloudreal.cbms.vo.Mediasumm;
import cn.cloudreal.cbms.vo.ProjectMedium;
import cn.cloudreal.cbms.vo.PutInPlanMonth;
import cn.cloudreal.cbms.vo.SourcePlanYear;
import cn.cloudreal.cbms.vo.UserInfo;

/**
 * <p>
 * Title: ITC
 * </p>
 * <p>
 * Description:资源状态dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author 夏加冬
 * @date 2012-7-9
 */
public class ResStateDaoImpl extends SqlMapClientDaoSupport implements
        ResStateDao
{
    private final String GET_ALL_PROJECTMEDIUM_BY_CONDITION = "projectmedium.getAllByCondition";
    private final String GET_ALL_PROJECTMEDIUM_COUNT_BY_CONDITION = "projectmedium.getAllCountByCond";
    private final String UPDATE_PROJECTMEDIUM_STATUS = "projectmedium.updateprojectmediumstatus";
    private final String GETUSERINFO = "userInfo.getUserInfoByUserCode";
    
    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(ResStateDaoImpl.class);

    /**
     * 查询列表数据
     * 
     * @param page
     * @return List<Sample>
     */
    @SuppressWarnings("unchecked")
    public List<ProjectMedium> getProjMediums(PageBean page,
            ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getProjMediums() method,return List<ProjectMedium>");
        }
        log.debug(page);
        Map param1 = new HashMap();
        param1.put("mediumStatus", projectMedium.getMediumStatus());
        if(projectMedium.getMediumName()!= null && projectMedium.getMediumName().length() !=0)
        {
            param1.put("mediumName", projectMedium.getMediumName());
        }
        if(projectMedium.getArrangeTime()!= null &&projectMedium.getArrangeTime().length()!=0)
        {
            param1.put("arrangeTime", projectMedium.getArrangeTime());
        }
        if (projectMedium.getProject() != null)
        {
            if (projectMedium.getProject().getCity() != null)
                param1.put("city", projectMedium.getProject().getCity());
            if (projectMedium.getProject().getProjectType() != null)
                param1.put("projectType", projectMedium.getProject()
                        .getProjectType());
            if (projectMedium.getProject().getProjectManager() != null && projectMedium.getProject().getProjectManager().length() !=0)
                param1.put("projectManager", projectMedium.getProject()
                        .getProjectManager());
            if (projectMedium.getProject().getProjectName() != null && projectMedium.getProject().getProjectName().length() !=0)
                param1.put("projectName", projectMedium.getProject()
                        .getProjectName());
            if (projectMedium.getProject().getProjectSection() != null && projectMedium.getProject().getProjectSection().length() !=0)
                param1.put("projectSection", projectMedium.getProject()
                        .getProjectSection());
        }
        if (projectMedium.getMediasumm() != null
                && projectMedium.getMediasumm().getMediaType() != null)
            param1.put("mediaType", projectMedium.getMediasumm()
                    .getMediaType());
        if(projectMedium.getCreator() !=null)
        {
            param1.put("creator", projectMedium.getCreator());
        }
        // 当前状态下的项目媒介条数
        int currentCount = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_PROJECTMEDIUM_COUNT_BY_CONDITION,
                param1);
        List<ProjectMedium> projectMediums = null;

        // 判断 totalCount 是否 >0
        if (currentCount > Constants.CONST_0)
        {
            page.setTotalRecords(currentCount);
            param1.put("pageNo", page.getPageNo());
            param1.put("pagesize", page.getPageSize());
            projectMediums = getSqlMapClientTemplate().queryForList(GET_ALL_PROJECTMEDIUM_BY_CONDITION,
                    param1);
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getProjMediums() method,return List<ProjectMedium>");
        }
        return projectMediums;
    }

    // 获取当前项目媒介的条数
    @SuppressWarnings("unchecked")
    public int getcurrentCount(ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getcurrentCount() method,return int");
        }
        Map param1 = new HashMap();
        param1.put("mediumStatus", projectMedium.getMediumStatus());
        if(projectMedium.getMediumName()!= null && projectMedium.getMediumName().length() !=0)
        {
            param1.put("mediumName", projectMedium.getMediumName());
        }
        if(projectMedium.getArrangeTime()!= null &&projectMedium.getArrangeTime().length()!=0)
        {
            param1.put("arrangeTime", projectMedium.getArrangeTime());
        }
        if (projectMedium.getProject() != null)
        {
            if (projectMedium.getProject().getCity() != null)
                param1.put("city", projectMedium.getProject().getCity());
            if (projectMedium.getProject().getProjectType() != null)
                param1.put("projectType", projectMedium.getProject()
                        .getProjectType());
            if (projectMedium.getProject().getProjectManager() != null && projectMedium.getProject().getProjectManager().length() !=0)
                param1.put("projectManager", projectMedium.getProject()
                        .getProjectManager());
            if (projectMedium.getProject().getProjectName() != null && projectMedium.getProject().getProjectName().length() !=0)
                param1.put("projectName", projectMedium.getProject()
                        .getProjectName());
            if (projectMedium.getProject().getProjectSection() != null && projectMedium.getProject().getProjectSection().length() !=0)
                param1.put("projectSection", projectMedium.getProject()
                        .getProjectSection());
        }
        if (projectMedium.getMediasumm() != null
                && projectMedium.getMediasumm().getMediaType() != null)
            param1.put("mediaType", projectMedium.getMediasumm()
                    .getMediaType());
        if(projectMedium.getCreator() !=null)
        {
            param1.put("creator", projectMedium.getCreator());
        }
        // 当前状态下的项目媒介条数
        int currentCount = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_PROJECTMEDIUM_COUNT_BY_CONDITION,
                param1);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getcurrentCount() method,return int");
        }
        return currentCount;
    }

    // 获取总的项目媒介条数
    @SuppressWarnings("unchecked")
    public int getsumCount(ProjectMedium projectMedium)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getcurrentCount() method,return int");
        }
        Map param1 = new HashMap();
        if(projectMedium.getMediumName()!= null && projectMedium.getMediumName().length() !=0)
        {
            param1.put("mediumName", projectMedium.getMediumName());
        }
        if(projectMedium.getArrangeTime()!= null &&projectMedium.getArrangeTime().length()!=0)
        {
            param1.put("arrangeTime", projectMedium.getArrangeTime());
        }
        if (projectMedium.getProject() != null)
        {
            if (projectMedium.getProject().getCity() != null)
                param1.put("city", projectMedium.getProject().getCity());
            if (projectMedium.getProject().getProjectType() != null)
                param1.put("projectType", projectMedium.getProject()
                        .getProjectType());
            if (projectMedium.getProject().getProjectManager() != null && projectMedium.getProject().getProjectManager().length() !=0)
                param1.put("projectManager", projectMedium.getProject()
                        .getProjectManager());
            if (projectMedium.getProject().getProjectName() != null && projectMedium.getProject().getProjectName().length() !=0)
                param1.put("projectName", projectMedium.getProject()
                        .getProjectName());
            if (projectMedium.getProject().getProjectSection() != null && projectMedium.getProject().getProjectSection().length() !=0)
                param1.put("projectSection", projectMedium.getProject()
                        .getProjectSection());
        }
        if (projectMedium.getMediasumm() != null
                && projectMedium.getMediasumm().getMediaType() != null)
            param1.put("mediaType", projectMedium.getMediasumm()
                    .getMediaType());
        if(projectMedium.getCreator() !=null)
        {
            param1.put("creator", projectMedium.getCreator());
        }
        // 当前状态下的项目媒介条数
        int sumCount = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_PROJECTMEDIUM_COUNT_BY_CONDITION,
                param1);
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter ResMonitorDaoImpl getcurrentCount() method,return int");
        }
        return sumCount;
    }

    /**
     * 设置项目媒介的状态
     * 
     * @param selectStrings
     */
    public void setProjectMediumStatus(String[] selectStrings,
            ProjectMedium projectMedium)
    {
        for (int i = 0; i < selectStrings.length; i++)
        {
            projectMedium.setProMedID(Integer.valueOf(selectStrings[i].trim()));
            getSqlMapClientTemplate().update(UPDATE_PROJECTMEDIUM_STATUS,
                    projectMedium);
        }
    }
    /**
     * 获取用户对象
     * 
     * @param code
     * @return
     */
    public UserInfo getUserInfo(String userCode)
    {
        return (UserInfo) getSqlMapClientTemplate().queryForObject(GETUSERINFO, userCode);
    }

}
